package com.dj.ms.configure;

import com.dj.ms.support.mq.RocketMQHandler;
import com.dj.ms.support.mq.MqProperties;
import com.dj.ms.support.transaction.TransactionMsgHandler;
import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import javax.annotation.Resource;

/**
 * <br>
 * <p>事务消息处理</p>
 *
 * <br>
 *
 * @author ZJG
 * @version 1.0
 * @date 2021/10/21 下午4:34
 */
@Data
@Order(Ordered.HIGHEST_PRECEDENCE-200)
@Configuration
@ConditionalOnClass(AliYunMQConfiguration.class)
public class MqAutoConfiguration {

    @Resource
    private MqProperties mqProperties;

    @Bean
    @ConditionalOnMissingBean(RocketMQHandler.class)
    public RocketMQHandler rocketMQHandler() {
        return new RocketMQHandler();
    }

    @Bean
    @ConditionalOnMissingBean(TransactionMsgHandler.class)
    public TransactionMsgHandler transactionMsgHandler() {
        if (!mqProperties.isEnableTransaction()) {
            return null;
        }
        return new TransactionMsgHandler();
    }

}
